package com.wtwd.device.util;



import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.stream.Stream;

/**
 * @author xsansan
 * @date 2018年9月11日 
 * Description: 工具类
 */
public class Tools 
{

	/**
	 * 判断字符串序列中是否有空元素
	 * @param args
	 * @return
	 */
	public static boolean stringIsBlank(String ...args)
	{
		for (String str : args)
		{
			if (StringUtils.isBlank(str))
			{
				return true;
			}
		}
		return false;	
	}
	
	public static boolean intIs0(int ...args)
	{
		for (int i : args)
		{
			if (i == 0)
			{
				return true;
			}
		}
		return false;	
	}

	
	public static <K extends Comparable<? super K>, V > Map<K, V> sortByKey(Map<K, V> map, boolean asc) 
	{
        Map<K, V> result = new LinkedHashMap<>();
        Stream<Entry<K, V>> stream = map.entrySet().stream();
        if (asc) 
		{
        	stream.sorted(Entry.comparingByKey())
                .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        }
        else 
		{
        	stream.sorted(Entry.<K, V>comparingByKey().reversed())
            	.forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
		}
	    return result;
	}

	/**
	 * map通过value排序
	 * @param map
	 * @param asc
	 * @return
	 */
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map, boolean asc) 
    {
        Map<K, V> result = new LinkedHashMap<>();
        Stream<Entry<K, V>> stream = map.entrySet().stream();    
		if (asc) 
		{
			 //stream.sorted(Comparator.comparing(e -> e.getValue()))
			stream.sorted(Entry.comparingByValue())
				.forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
		}
		else 
		{
			 //stream.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
			stream.sorted(Entry.<K, V>comparingByValue().reversed())
				.forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
		}
        return result;
    }
    
    /**
	 * 删除字符串尾部的0
	 * @param str
	 * @return String
	 */
	public static String removeTail0(String str)
	{
		//如果字符串尾部不为0，返回字符串
		if(!"0".equals(str.substring(str.length() - 1)))
		{
			return str;
		}
		else
		{
			//否则将字符串尾部删除一位再进行递归
			return removeTail0(str.substring(0, str.length() - 1));
		}
	}
	
	/**
	 * 字符串按两位倒序
	 * @param str
	 * @return
	 */
	public static String string2Reverse(String str){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
        	int s1 = str.length() - i - 2;
        	int s2 = str.length() - i;
			sb.append(str.substring(s1, s2));
			i++;
		}
        return sb.toString();
    }
	
	/**
	 * 获取6位随机数字
	 * @return
	 */
	public static String getRamdom6() {
		return String.valueOf((int)((Math.random()*9+1)*100000));
	}
	
	/**
	 * 使用UUID生成随机字符串
	 * @return
	 */
	public static String getRamdomStr() {		
		String str = UUID.randomUUID().toString().replace("-", "");
		return str;
	}
	
	/**
	 * 使用UUID生成随机字符串,取24-36位
	 * @return
	 */
	public static String getRamdomShareCode() {		
		String str = UUID.randomUUID().toString().substring(24);
		return str;
	}
	
	/**
	 * 循环List元素以逗号分隔
	 * @param list<String> list
	 * @return String
	 */
    public static String listToString(List<String> list) {
    	if (list.isEmpty())
			return null;
		StringBuilder sb = new StringBuilder();
		list.forEach(ls -> sb.append(ls).append(","));
		String str = sb.toString();
		return str.substring(0, str.length()-1);
    }

    /**
     * 计算a占b的百分比，结果乘100
     *
     * @param a a
     * @param b b
     * @return Percent
     */
    public static int calPercentage(int a, int b) {
        if (b == 0)
            return 0;
        BigDecimal bd1 = new BigDecimal(String.valueOf(a));
        BigDecimal bd2 = new BigDecimal(String.valueOf(b));
        BigDecimal multiplier = new BigDecimal(Float.toString(100f));
        BigDecimal divide = bd1.divide(bd2, 2, RoundingMode.HALF_UP);
        return (int) divide.multiply(multiplier).floatValue();
    }


 
}
